package lx.prac.java8.stream;

import lx.prac.java8.ch4.Dish;
import org.apache.commons.compress.utils.Lists;

import java.util.*;

public class Person {
    enum Sex {Male, Female}
    private String name;
    private int age;
    private Sex sex;

    public Person(String name, int age, Sex sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("name=").append(this.name).append(",");
        sb.append("age=").append(this.age).append(",");
        sb.append("sex=").append(this.sex.name()).append(",");

        return sb.toString();
    }

    /**
     * 1.年龄大于40岁的女性。2.年龄从小到大排序。3.前10个Person对象。
     * @param list
     * @param age
     * @param sexType
     */
    public static void defaultOption(List<Person> list, int age, Sex sexType) {
        List<Person> newList = new ArrayList<>();

        for (Person p : list) {
            if (p.age > age && p.sex == Sex.Female) {
                newList.add(p);
            }

            newList.sort(Comparator.comparingInt(p2 -> p2.age));
        }

        for (int i = 0; i < 3; i++) {
            System.out.println(newList.get(i));
        }
    }

    public static void streamOption(List<Person> list, int age, Sex sexType) {
        list.stream().filter(p -> (p.age > age && p.sex == Sex.Female)).sorted((p1,p2) -> p1.age - p2.age).limit(3)
        .forEach(System.out::println);
    }

    public static void main(String[] args) {
        List<Person> list = Arrays.asList(
                new Person("zs", 10, Sex.Male),
                new Person("ls", 20, Sex.Female),
                new Person("ww", 30, Sex.Male),
                new Person("zl", 40, Sex.Female),
                new Person("tq", 50, Sex.Male),
                new Person("wb", 60, Sex.Female),
                new Person("pj", 70, Sex.Male),
                new Person("111", 80, Sex.Female),
                new Person("222", 90, Sex.Male),
                new Person("33s", 110, Sex.Female),
                new Person("444", 100, Sex.Male)
        );

        System.out.println("******************default option*******************");
        defaultOption(list,40,Sex.Female);

        System.out.println("******************stream option*******************");
        streamOption(list,40,Sex.Female);

    }

}
